package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insertSetmeal(setmeal);

        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();


        for (SetmealDish setmealDish : setmealDishes) {
//            Long dishId = setmealDish.getDishId();
//            Dish dish = dishMapper.searchDishById(dishId);
//            if(dish.getStatus() == StatusConstant.DISABLE){
//                throw new BaseException(MessageConstant.DISH_NOT_SALE);
//            }
            setmealDish.setSetmealId(id);
            setmealDishMapper.insertSetmealDish(setmealDish);
        }

    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult searchSetmeals(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.searchSetmeals(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO searchSetmealById(Long id) {
        Setmeal setmeal = setmealMapper.searchSetmealById(id);
        List<SetmealDish> list = setmealDishMapper.searchSetmealDishBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }


    /**
     * 更新套餐
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updateSetmeal(setmeal);

        //更新套餐菜品关系表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes == null || setmealDishes.isEmpty()) {
            throw new BaseException(MessageConstant.SETMEAL_IS_NULL);
        }
        Long id = setmealDTO.getId();
        //先删除数据库的旧套餐菜品关系
        setmealDishMapper.deleteSetmealDishBySetmealId(id);
        for (SetmealDish setmealDish : setmealDishes) {
//            Long dishId = setmealDish.getDishId();
//            Dish dish = dishMapper.searchDishById(dishId);
//            if(dish.getStatus() == StatusConstant.DISABLE){
//                throw new BaseException(MessageConstant.DISH_NOT_SALE);
//            }
            //先获取套餐id再存套餐菜品关系
            setmealDish.setSetmealId(id);
            setmealDishMapper.insertSetmealDish(setmealDish);
        }

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteSetmeals(List<Long> ids) {
        for (Long id : ids) {
            //判断当前套餐是否能够删除---是否存在起售中的套餐？？
            Setmeal setmeal = setmealMapper.searchSetmealById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                //当前套餐处于起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐
        setmealMapper.deleteSetmeals(ids);

        //根据套餐id删除套餐菜品关系
        for (Long id : ids) {
            setmealDishMapper.deleteSetmealDishBySetmealId(id);
        }

    }


    /**
     * 更新套餐状态
     * @param status
     * @param id
     */
    @CacheEvict(value = "SetmealCache" , allEntries = true)
    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        if (status.equals(StatusConstant.ENABLE)) {
            List<Map> dishStatuss = setmealMapper.searchDishStatusBySetmealId(id);
            dishStatuss.forEach(dishStatus ->{
                if(dishStatus.get("dishStatus").equals(StatusConstant.DISABLE)){
                    throw new BaseException(MessageConstant.DISH_NOT_SALE);
                }
            });
        }
        //菜品都为起售才能起售套餐
        setmealMapper.updateSetmeal(setmeal);
    }

    @Override
    public List<SetmealVO> searchSetmealByCategoryId(Long categoryId) {

        List<SetmealVO> setmeales =  setmealMapper.searchSetmealByCategoryId(categoryId);

        return setmeales;
    }

    @Override
    public List<DishItemVO> searchSemealDishBySetmealId(Long id) {
        List<DishItemVO> setmealDishes = setmealDishMapper.searchSetmealDishPlusBySetmealId(id);
        return setmealDishes;
    }
}